<?php

namespace app\models\product;

use Yii;

/**
 * This is the model class for table "product_area_list".
 *
 * @property int $id
 * @property int $product_id
 * @property int $area_id
 */
class ProductAreaList extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'product_area_list';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['product_id', 'area_id'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'product_id' => 'Product ID',
            'area_id' => 'Area ID',
        ];
    }

    /**
     * with area
     *
     * @return \yii\db\ActiveQuery
     */
    public function getName()
    {
        return $this->hasOne(ProductArea::class, ['id' => 'area_id']);
    }

    /**
     * get product area list
     *
     * @param $product_id
     * @return array
     */
    public static function getProductAreaList($product_id)
    {
        return self::find()
            ->where(['product_id' => $product_id])
            ->select(['area_id'])
            ->column();
    }

    /**
     * check in area by product id
     *
     * @param $product_id
     * @param $area_map_value
     * @return bool
     */
    public static function checkInArea($product_id, $area_map_value)
    {
        $area_ids = self::getProductAreaList($product_id);

        return in_array($area_map_value, $area_ids);
    }
}
